<?php

declare(strict_types=1);

namespace App\Hoenix\Model;

use App\Fastapp\Model\FastappRobotGroup;
use Hyperf\Database\Model\Relations\HasOne;
use Mine\MineModel;

/**
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property int $id 主键
 * @property int $chat_id
 * @property int $status
 * @property int $type 0用户1群2频道3视频4音频5图片6文本7连接8网盘
 * @property int $sort
 * @property int $msg_id
 * @property string $c_at
 * @property string $title
 * @property string $url
 * @property string $username
 * @property-read null|FastappRobotGroup $tgChat
 * @property-read null|HoenixIndex $msgNum
 */
class HoenixIndex extends MineModel
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'hoenix_index';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['created_at', 'updated_at', 'id', 'chat_id', 'status', 'type', 'sort', 'msg_id', 'c_at', 'title', 'url', 'username'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['created_at' => 'datetime', 'updated_at' => 'datetime', 'id' => 'integer', 'chat_id' => 'integer', 'status' => 'integer', 'type' => 'integer', 'sort' => 'integer', 'msg_id' => 'integer'];

    public function tgChat(): HasOne
    {
        return $this->hasOne(FastappRobotGroup::class, 'group_id', 'chat_id')->where(['source' => 1]);
    }

    public function msgNum(): HasOne
    {
        return $this->hasOne(HoenixIndex::class, 'chat_id', 'chat_id')
            ->whereIn('type', [3, 4, 5, 6])->selectRaw('chat_id,count(*) as num');
    }
}
